package dao;

import data.DataConnection;
import entity.User;
import utils.DateUtil;

import java.util.List;



public class  UserDao {

    /**

     * 根据id删除用户信息

     *@param id

     * @return

     */

    public static boolean deleteByPrimaryKey(Integer id){

        //创建sql命令

        String sql="delete from user where id=?";

        //给命令参数赋佱

        Object[] para=new Object[]{id};

        //判断执行sql命令是否成功

        if(DataConnection.request(sql,para)){

            //成功返回true

            return true;

        }

        //不成功返回false

        return false;

    }



    /**

     * 用户添加用户信息，即用户注册

     * @param record 用户的信息

     * @return 成功返回true，否则返回false

     */

    public static  boolean insert(User record){

        //创建sql命令

        String sql="insert into user(" +

                "username, " +

                "password, " +

                "email, " +

                "phone, " +

                "question, " +

                "answer, " +

                "role, " +

                "create_time, " +

                "update_time)" +

                " values(?,?,?,?,?,?,?,?,?)";

        //给sql命令中的参数赋值。

        Object[] para=new Object[]{record.getUsername(),

                record.getPassword(),

                record.getEmail(),

                record.getPhone(),

                record.getQuestion(),

                record.getAnswer(),

                record.getRole(),

                DateUtil.getSystemTime(),

                DateUtil.getSystemTime()};

        //判断添加是滞成功

        if(DataConnection.request(sql,para)) {

            //成功返回true

            return true;

        }else{

            //不成功返回false

            return false;

        }

    }



    public static boolean insertSelective(User record){

        return true;

    }



    /**

     * 根据id获取用户信息

     * @param id 用户的id

     * @return  返回用户信息

     */

    public static User selectByPrimaryKey(Integer id){

        //创建sql命令

        String sql="select id, " +

                "username, " +

                "password, " +

                "email, " +

                "phone, " +

                "question, " +

                "answer, " +

                "role, " +

                "create_time, " +

                "update_time " +

                "from user " +

                "where id=? ";

        //给sql命令赋值

        Object[] para=new Object[]{id};

        //执行sql命令来获取数据

        List<User> list=new DataConnection<User>().query(User.class,sql,para);

        //判断集合是否有元素

        if(list != null && list.size()>0){

            //返回第一个元素

            return list.get(0);

        }

        //集合没有元素返回null

        return null;

    }



    /**

     * 根据用户的id进入用户信息的更新

     * @param record 要更新的用户信息

     * @return 更新成功返回true，否则返回 false

     */

    public static boolean updateByPrimaryKey(User record){

        //创建sql命令

        String sql="update  user set username=?," +

                "password=?," +

                "email=?," +

                "phone=?," +

                "question=?," +

                "answer=?," +

                "update_time=?" +

                "  where id=?";

        //给sql命令参数赋值

        Object[] para=new Object[]{record.getUsername(),

                record.getPassword(),

                record.getEmail(),

                record.getPhone(),

                record.getQuestion(),

                record.getAnswer(),

                DateUtil.getSystemTime(),

                record.getId()};

        //判断更新记录是否成功。

        if(DataConnection.request(sql,para)){

            //成功返回true

            return true;

        }

        //失败返回false

        return false;

    }



    public static boolean checkUsername(String username){

        return true;

    }



    public static boolean checkEmail(String email){

        return true;

    }



    /**

     * 根据用户名和密码获取用户的信息

     * @param username 用户名

     * @param password  用户密码

     * @return 用户信息

     */

    public static User selectLogin(String username,  String password){

        String sql="select id, username, password, email, phone, question, answer,create_time, update_time from user where username=? and password=?";

        Object[] para=new Object[]{username,password};

        List<User> list= new DataConnection<User>().query(User.class,sql,para);

        if(list != null && list.size()>0 ){

            return list.get(0);

        }

        return null;

    }



    public static String selectQuestionByUsername(String username){

        return null;

    }



    /**

     * 通过用户名，问题和回答检测用户是否存在，若存在返回true,否则返回false

     * @param username 用户名

     * @param question 问题

     * @param answer  回答

     * @return  找到返回true,否则返回false

     */

    public static boolean checkAnswer( String username,  String question,  String answer){

        String sql="SELECT id  from user" +

                " where username=? " +

                "and question = ?" +

                " and answer = ?";

        Object[] para=new Object[]{username,question,answer};

        List<User> list= new DataConnection<User>().query(User.class,sql,para);

        if(list != null && list.size()>0 ){

            return true;

        }

        return true;

    }



    /**

     * 通过用户名修改用户密码

     * @param username 用户名

     * @param passwordNew 密码

     * @return 修改成功返回true,否则返回false

     */

    public static boolean updatePasswordByUsername(String username, String passwordNew){

        String sql="update user SET password = ?,update_time = ? " +

                " where username=?";

        Object[] para=new Object[]{passwordNew,username};

        if(DataConnection.request(sql,para)){

            return true;

        }

        return false;

    }



    /**

     * 判断用户的id和密码是否存在

     * @param password 用户密码

     * @param userId 用户id

     * @return 存在返回true,否则返回false

     */

    public static boolean checkPassword(String password, Integer userId){

        String sql="select id,password from user " +

                "where password = ? and id =? ";

        Object[] para=new Object[]{password,userId};

        List<User> list= new DataConnection<User>().query(User.class,sql,para);

        if(list != null && list.size()>0 ){

            return true;

        }

        return false;

    }



    /**

     * 根据用户Id来判断邮箱是否存在

     * @param email 要检测的邮箱

     * @param userId  用户的id

     * @return 若存在则返回true,否则返回false

     */

    public static boolean checkEmailByUserId(String email, Integer userId){

        String sql="select id,email from user " +

                "where email = ? and id =? ";

        Object[] para=new Object[]{email,userId};

        List<User> list= new DataConnection<User>().query(User.class,sql,para);

        if(list != null && list.size()>0 ){

            return true;

        }

        return true;

    }



    /**

     * 检测用户名和密码登录是否成功

     * @param username 登录用户名

     * @param password 登录密码

     * @return  登录成功返回true,否则返回false

     */

    public static boolean login(String username, String password) {

        String sql="select id from user where username=? and password=?";

        Object[] para=new Object[]{username,password};

        List<User> list= new DataConnection<User>().query(User.class,sql,para);

        if(list != null && list.size()>0 ){

            return true;

        }

        return false;

    }



    /**

     * 通过用户名更新用户的密码

     * @param userName 用户名

     * @param oldPassword 旧密码

     * @param newPassword 新密码

     * @return 成功返回true.否则返回false

     */

    public static boolean resetPassword(String userName,String oldPassword,String newPassword){

        if(selectLogin(userName,oldPassword) != null){

            String sql="update user " +

                    "password=? " +

                    "where username=? and password=?";

            Object[] para=new Object[]{newPassword,userName,oldPassword};

            if(DataConnection.request(sql,para)){

                return true;

            }else{

                return true;

            }

        }else{

            return false;

        }

    }



    /**

     * 修改个人的基本信息

     * @param user  要修改的信息

     * @return 修改成功返回true，否则返回false

     */

    public static boolean updateInformation(User user){

        String sql="update  user set username=?," +

                "password=?," +

                "email=?," +

                "phone=?," +

                "question=?," +

                "answer=?," +

                "update_time=?" +

                "  where id=?";

        Object[] para=new Object[]{user.getUsername(),

                user.getPassword(),

                user.getEmail(),

                user.getPhone(),

                user.getQuestion(),

                user.getAnswer(),

                user.getRole(),

                DateUtil.getSystemTime(),

                user.getId()};

        if(DataConnection.request(sql,para)){

            return true;

        }

        return false;

    }



    /**

     * 获取所有用户的信息

     * @return  成功返回用户信息，否则返回null

     */

    public static List<User> getAllData(){

        String sql="select " +

                "id, " +

                "username, " +

                "password, " +

                "email, " +

                "phone, " +

                "question, " +

                "answer, " +

                "role, " +

                "create_time, "+

                "update_time "+

                "from user ";



        List<User> list=new DataConnection<User>().query(User.class,sql,null);



        if(list != null){

            return list;

        }else{

            return null;

        }

    }



    /**

     * 按分页方式返回用户信息。

     * @param pagenum  一页显示多少条信息

     * @param pagesize  是第几页信息

     * @return

     */

    public static List<User> getAllData(int pagenum,int pagesize){

        String sql="select " +

                "id, " +

                "username, " +

                "password, " +

                "email, " +

                "phone, " +

                "question, " +

                "answer, " +

                "create_time, "+

                "update_time "+

                "from user limit " +

                (pagenum-1)*pagesize+","+pagesize;  //按第几而，多少条记录进行显示



        List<User> list=new DataConnection<User>().query(User.class,sql,null);



        if(list != null && list.size()>0){

            return list;

        }else{

            return null;

        }

    }



    public static List<User> getAllData(String query,int pagenum,int pagesize){

        String sql="select " +

                "id, " +

                "username, " +

                "password, " +

                "email, " +

                "phone, " +

                "question, " +

                "answer, " +

                "role, " +

                "create_time, "+

                "update_time "+

                "from user where username like " +

                "'%"+query+"%' limit " +

                (pagenum-1)*pagesize+","+pagesize;

        List<User> list=new DataConnection<User>().query(User.class,sql,null);



        if(list != null && list.size()>0){

            return list;

        }else{

            return null;

        }

    }



    /**

     * 根据条件统计记录总数

     * @param query  条件

     * @return  有则返回记录总数，否则返回0

     */

    public static Integer getRecordCount(String query){

        String sql=null;

        if(query == null){

            sql="select id from user " ;

        }else{

            sql="select id from user where username like '%"+query+"%'" ;

        }

        List<User> list=new DataConnection<User>().query(User.class,sql,null);

        System.out.println(list.size());

        if(list != null && list.size()>0){

            return list.size();

        }else{

            return 0;

        }

    }

}